/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "UpdateIconTask.h"
#include "../../Controls/Application.h"
#include "../../Prompts/ProgressWindow.h"
#include "../../Prompts/PromptWindows.h"
#include "../../FileOperations/fileops.h"
#include "../FileDownloader.h"

UpdateIconTask::UpdateIconTask()
{
	result = false;
	running = true;
	
	ThreadedTaskHandler::Instance()->AddTask(this);
}

void UpdateIconTask::Execute(void)
{
	if(!CreateSubfolder(Settings.UpdatePath))
	{
		ShowError(tr("Could not create path: %s"), Settings.UpdatePath);
		running = false;
		return;
	}

	if(!NetworkInitPrompt())
	{
		ShowError(tr("Network is not initiated."));
		running = false;
		return;
	}
	
	if(ProgressWindow::Instance()->IsRunning())
		ProgressWindow::Instance()->SetTitle(tr("Updating icon.png..."));
	else
		StartProgress(tr("Updating icon.png..."));

	std::string iconUrl = fmt("http://savegame-manager-gx.googlecode.com/svn/trunk/HBC/icon%d.png", Settings.UpdateIcon);
	std::string iconPath = Settings.UpdatePath;
	iconPath += "/icon.png";
	
	int ret = DownloadFileToPath(iconUrl.c_str(), iconPath.c_str(), false);
	if(ret < 1024)
	{
		StopProgress();
		ShowError(tr("Error when downloading file: %i"), ret);
		running = false;
		return;
	}
	sleep(1);
	
	result = true;
	running = false;
}
